<template>
  <div class="login-container">
    <div class="login-box">
      <div class="title-container">
        <h2>用户注册</h2>
        <div class="title-underline"></div>
      </div>
      <div class="login-form">
        <el-form ref="registerFormRef" :model="registerForm" :rules="registerRules">
          <el-form-item prop="username">
            <el-input v-model="registerForm.username" placeholder="请输入用户名" prefix-icon="User" />
          </el-form-item>

          <el-form-item prop="password">
            <el-input v-model="registerForm.password" type="password" placeholder="请输入密码" prefix-icon="Lock"
              show-password />
          </el-form-item>

          <el-form-item prop="confirmPassword">
            <el-input v-model="registerForm.confirmPassword" type="password" placeholder="请确认密码" prefix-icon="Lock"
              show-password />
          </el-form-item>



          <el-form-item>
            <el-button type="primary" @click="handleRegister" style="width: 100%">
              注册
            </el-button>
          </el-form-item>

          <div class="login-link">
            已有账号？
            <el-link type="primary" @click="goToLogin">返回登录</el-link>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup>
import { registerAPI } from '@/api/login'
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
const router = useRouter()
const registerForm = reactive({
  username: '',
  password: '',
  confirmPassword: '',
})
const registerFormRef = ref(null)

const validatePass = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请输入密码'))
  } else {
    if (registerForm.confirmPassword !== '') {
      registerFormRef.value.validateField('confirmPassword')
    }
    callback()
  }
}

const validatePass2 = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请再次输入密码'))
  } else if (value !== registerForm.password) {
    callback(new Error('两次输入密码不一致!'))
  } else {
    callback()
  }
}

const registerRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, validator: validatePass, trigger: 'blur' },
    { min: 6, message: '密码长度至少为 6 个字符', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, validator: validatePass2, trigger: 'blur' }
  ],
}

const handleRegister = async () => {
  // 这里添加注册逻辑
  const { data: res } = await registerAPI(registerForm)
  console.log(res);
  if (res.status !== 0) {

    // eslint-disable-next-line no-undef
    return ElMessage({
      showClose: true,
      message: res.message,
      type: 'error',
    })
  }
  // eslint-disable-next-line no-undef
  ElMessage.success(res.message)
  router.push('/login')
}

const goToLogin = () => {
  router.push('/login')
}
</script>

<style scoped>
.login-container {
  height: 100vh;
  width: 100vw;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  position: relative;
  overflow: hidden;
  margin: 0;
  padding: 0;
}

.login-container::before {
  content: '';
  position: absolute;
  width: 200%;
  height: 200%;
  background: rgba(255, 255, 255, 0.1);
  transform: rotate(-45deg);
  animation: wave 8s infinite linear;
}

.login-box {
  width: 420px;
  padding: 40px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  position: relative;
  transform: translateY(0);
  transition: all 0.3s ease;
}

.login-box:hover {
  transform: translateY(-5px);
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.15);
}

.title-container {
  text-align: center;
  margin-bottom: 35px;
}

.title-container h2 {
  color: #333;
  font-size: 28px;
  font-weight: 600;
  margin-bottom: 10px;
}

.title-underline {
  width: 60px;
  height: 3px;
  background: linear-gradient(90deg, #667eea, #764ba2);
  margin: 0 auto;
  border-radius: 3px;
}

.login-form {
  margin-top: 25px;
}

:deep(.el-input .el-input__wrapper) {
  background: rgba(255, 255, 255, 0.8);
  border: 1px solid #dcdfe6;
  border-radius: 8px;
  transition: all 0.3s ease;
}

:deep(.el-input .el-input__wrapper:hover) {
  border-color: #764ba2;
  box-shadow: 0 0 0 1px rgba(118, 75, 162, 0.1);
}

:deep(.el-button--primary) {
  background: linear-gradient(90deg, #667eea, #764ba2);
  border: none;
  height: 44px;
  font-size: 16px;
  border-radius: 8px;
  transition: all 0.3s ease;
}

:deep(.el-button--primary:hover) {
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
}

.login-link {
  text-align: center;
  margin-top: 15px;
  font-size: 14px;
  color: #606266;
}

:deep(.el-link) {
  margin-left: 5px;
  font-size: 14px;
  transition: all 0.3s ease;
}

:deep(.el-link:hover) {
  transform: translateY(-1px);
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

@keyframes wave {
  0% {
    transform: rotate(-45deg) translate(-50%, -50%);
  }

  100% {
    transform: rotate(-45deg) translate(50%, 50%);
  }
}

/* 添加输入框动画效果 */
:deep(.el-input__wrapper) {
  animation: fadeInUp 0.5s ease-out;
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}
</style>